package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.impl.ClaimChartQueryObject;
import cn.wolfcode.crm.query.impl.SaleChartQueryObject;
import cn.wolfcode.crm.service.IChartService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IChartService chartService;

    @RequestMapping("listSale")
    public String listSale(Model model) {
        Map<String, String> typeMap = SaleChartQueryObject.typeMap;
        model.addAttribute("typeMaps", typeMap);
        return "listSale";
    }

    @RequestMapping("listClaim")
    public String listClaim(Model model) {
        Map<String, String> typeMap = ClaimChartQueryObject.typeMap;
        model.addAttribute("typeMaps", typeMap);
        return "listClaim";
    }

    @RequestMapping("querySale")
    @ResponseBody
    public Object querySale(SaleChartQueryObject qo) throws Exception {
        List<Map<String, Object>> chartMaps = chartService.querySaleChart(qo);
        return chartMaps;
}

    @RequestMapping("queryClaim")
    @ResponseBody
    public Object queryClaim(ClaimChartQueryObject qo) throws Exception {
        List<Map<String, Object>> chartMaps = chartService.queryClaimChart(qo);
        return chartMaps;
    }

    @RequestMapping("saleByBar")
    public String saleByBar(Model model, @ModelAttribute("qo") SaleChartQueryObject qo) throws Exception {
        List<Object> x = new ArrayList<>();//用来显示X轴名称
        List<Object> y = new ArrayList<>();
        List<Map<String, Object>> maps = chartService.querySaleChart(qo);//根据类型分组,查询出分组后的数据
        for (Map<String, Object> chartMap : maps) {
            x.add(chartMap.get("groupType").toString());//需要转换成字符串格式
            y.add(chartMap.get("totalAmount").toString());
        }
        //拿到封装的map,通过key拿到value,共享用于子标题回显名字
        Map<String, String> typeMap = SaleChartQueryObject.typeMap;
        model.addAttribute("x", JSON.toJSONString(x));//共享分组类型列
        model.addAttribute("y", JSON.toJSONString(y));//共享总金额
        model.addAttribute("groupType", typeMap.get(qo.getGroupType()));//用户页面子标题回显类型对应的值
        return "saleByBar";
    }

    @RequestMapping("saleByPie")
    public String saleByPie(Model model, @ModelAttribute("qo") SaleChartQueryObject qo) throws Exception {
        List<Object> typeName = new ArrayList<>();//用来显示X轴名称
        List<Map<String, Object>> totalAmount = new ArrayList<>();
        List<Map<String, Object>> ChartMaps = chartService.querySaleChart(qo);//根据类型分组,查询出分组后的数据
        for (Map<String, Object> chartMap : ChartMaps) {
            Map<String, Object> saleAmount = new HashMap<>();
            typeName.add(chartMap.get("groupType").toString());//共享分组类型
            saleAmount.put("name", chartMap.get("groupType").toString());//设置name
            saleAmount.put("value", chartMap.get("totalAmount").toString());//设置value
            totalAmount.add(saleAmount);
        }

        //拿到封装的map,通过qo(页面)传过来的key拿到value,共享用于子标题回显类型名字
        Map<String, String> typeMap = SaleChartQueryObject.typeMap;
        model.addAttribute("typeName", JSON.toJSONString(typeName));//共享分组类型列
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmount));//共享总金额
        model.addAttribute("groupType", typeMap.get(qo.getGroupType()));//用户页面子标题回显类型名称
        //qo中包含从sale.jsp提交过来的类型名称
        return "saleByPie";
    }

    @RequestMapping("claimByBar")
    public String saleByBar(Model model, @ModelAttribute("qo") ClaimChartQueryObject qo) throws Exception {
        List<Object> typeName = new ArrayList<>();//用来显示X轴名称
        List<Object> totalAmount = new ArrayList<>();
        List<Map<String, Object>> ChartMaps = chartService.queryClaimChart(qo);//根据类型分组,查询出分组后的数据
        for (Map<String, Object> chartMap : ChartMaps) {
            typeName.add(chartMap.get("groupType").toString());//需要转换成字符串格式
            totalAmount.add(chartMap.get("totalAmount").toString());
        }
        //拿到封装的map,通过key拿到value,共享用于子标题回显名字
        Map<String, String> typeMap = ClaimChartQueryObject.typeMap;
        model.addAttribute("typeName", JSON.toJSONString(typeName));//共享分组类型列
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmount));//共享总金额
        model.addAttribute("groupType", typeMap.get(qo.getGroupType()));//用户页面子标题回显类型对应的值
        return "claimByBar";
    }

    @RequestMapping("claimByPie")
    public String saleByPie(Model model, @ModelAttribute("qo") ClaimChartQueryObject qo) throws Exception {
        List<Object> typeName = new ArrayList<>();//用来显示X轴名称
        List<Map<String, Object>> totalAmount = new ArrayList<>();
        List<Map<String, Object>> claimChartMaps = chartService.queryClaimChart(qo);//根据类型分组,查询出分组后的数据
        for (Map<String, Object> claimChart : claimChartMaps) {
            Map<String, Object> saleAmount = new HashMap<>();
            typeName.add(claimChart.get("groupType").toString());//共享分组类型
            saleAmount.put("name", claimChart.get("groupType").toString());//设置name
            saleAmount.put("value", claimChart.get("totalAmount").toString());//设置value
            totalAmount.add(saleAmount);
        }

        //拿到封装的map,通过qo(页面)传过来的key拿到value,共享用于子标题回显类型名字
        Map<String, String> typeMap = ClaimChartQueryObject.typeMap;
        model.addAttribute("typeName", JSON.toJSONString(typeName));//共享分组类型列
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmount));//共享总金额
        model.addAttribute("groupType", typeMap.get(qo.getGroupType()));//用户页面子标题回显类型名称
        //qo中包含从sale.jsp提交过来的类型名称
        return "claimByPie";
    }
}
